#include "regidialog.h"

regidialog::regidialog(QWidget *parent)
	: QDialog(parent)
{
	okButtonClick = false;
	cancelButtonClick = false;
	
	ui.setupUi(this);
	this->setWindowTitle(QString::fromUtf8("Choose images"));
	this->resize(425,160);
	createInterface();
	connectSlots();
}

regidialog::~regidialog()
{

}

void regidialog::createInterface()
{
	image1Label = new QLabel(this);
	image1Label->setObjectName(QString::fromUtf8("First image"));
	image1Label->setText(QString::fromUtf8("First image"));
	image1Label->setGeometry(QRect(30, 40, 94, 12));
	image2Label = new QLabel(this);
	image2Label->setObjectName(QString::fromUtf8("Second image"));
	image2Label->setText(QString::fromUtf8("Second image"));
	image2Label->setGeometry(QRect(30, 80, 94, 12));
	image1LineEdit = new QLineEdit(this);
	image1LineEdit->setObjectName(QString::fromUtf8("lineEdit"));
    image1LineEdit->setGeometry(QRect(110, 40, 221, 20));
	image2LineEdit = new QLineEdit(this);
	image2LineEdit->setObjectName(QString::fromUtf8("lineEdit_2"));
    image2LineEdit->setGeometry(QRect(110, 80, 221, 20));
	image1PushButton = new QPushButton(this);
	image1PushButton->setObjectName(QString::fromUtf8("pushButton"));
	image1PushButton->setText(QString::fromUtf8("Browse"));
    image1PushButton->setGeometry(QRect(350, 40, 50, 23));
	image2PushButton = new QPushButton(this);
	image2PushButton->setObjectName(QString::fromUtf8("pushButton_2"));
	image2PushButton->setText(QString::fromUtf8("Browse"));
    image2PushButton->setGeometry(QRect(350, 80, 50, 23));
	okPushButton = new QPushButton(this);
	okPushButton->setObjectName(QString::fromUtf8("pushButton_3"));
	okPushButton->setText(QString::fromUtf8("Ok"));
    okPushButton->setGeometry(QRect(100, 120, 75, 23));
	//okPushButton->setGeometry(QRect(180, 120, 75, 23));
	
    cancelPushButton = new QPushButton(this);
    	cancelPushButton->setObjectName(QString::fromUtf8("pushButton_4"));
    	cancelPushButton->setText(QString::fromUtf8("Cancel"));
        cancelPushButton->setGeometry(QRect(260, 120, 75, 23));
    

}

void regidialog::connectSlots()
{
	connect(image1PushButton,  SIGNAL(clicked()), this, SLOT(inputImage1()));
	connect(image2PushButton,  SIGNAL(clicked()), this, SLOT(inputImage2()));
	connect(okPushButton, SIGNAL(clicked()), this, SLOT(okClick()));
	connect(cancelPushButton, SIGNAL(clicked()), this, SLOT(cancelClick()));
}
/*
*	When multiple files are selected, first file will be put in the QLineEdit 
*	related to the Browse button pushed, and the second file will be put in the other QLineEdit.
*/
void regidialog::openImage(QLineEdit *lineEditMain,QLineEdit *lineEditBackup)
{
	
	const QStringList fileNames = QFileDialog::getOpenFileNames(this,tr("Open Image"),QDir::currentPath(),tr("Image Files (*.png *.jpg *.bmp)"));

	if(fileNames.length() == 1)
		lineEditMain->setText(fileNames[0]);
	else
	{
		if(fileNames.length()>1)
		{
			lineEditMain->setText(fileNames[0]);
			lineEditBackup->setText(fileNames[1]);
		}
	}
}


//slots
void regidialog::inputImage1()
{
	openImage(image1LineEdit,image2LineEdit);
	
}

void regidialog::inputImage2()
{
	openImage(image2LineEdit,image1LineEdit);
	
}

void regidialog::okClick()
{
	while (image1LineEdit->text() == NULL ||  image2LineEdit->text() == NULL)
	{
		QMessageBox::about(0,"ERROR!","Please choose two images!");
		return;
	}
	image1FilePath = image1LineEdit->text();
	image2FilePath = image2LineEdit->text();

	okButtonClick = true;
	accept();
}

void regidialog::cancelClick()
{
	//QMessageBox::information(this, tr("Plug & Paint"), tr("cancel button click"));
	reject();
}

RegistrationOpenDialog::RegistrationOpenDialog(QWidget *parent):QDialog(parent)
{

	resize(373, 134);
	isOKButtonClicked = false;
	this->setMinimumSize(QSize(373, 134));
	this->setMaximumSize(QSize(373, 134));
	label = new QLabel(this);
	label->setObjectName(QString::fromUtf8("label"));
	label->setGeometry(QRect(20, 20, 91, 16));
	lineEdit = new QLineEdit(this);
	lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
	lineEdit->setGeometry(QRect(20, 50, 261, 20));
	toolButton = new QToolButton(this);
	toolButton->setObjectName(QString::fromUtf8("toolButton"));
	toolButton->setGeometry(QRect(290, 50, 60, 21));
	buttonBox = new QDialogButtonBox(this);
	buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
	buttonBox->setGeometry(QRect(110, 90, 156, 23));
	buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);

	setWindowTitle(QApplication::translate("RegistrationOpenDialog", "Registration Open", 0, QApplication::UnicodeUTF8));
	label->setText(QApplication::translate("RegistrationOpenDialog", "Project Path\357\274\232", 0, QApplication::UnicodeUTF8));
	toolButton->setText(QApplication::translate("RegistrationOpenDialog", "Browse...", 0, QApplication::UnicodeUTF8));

	QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(OKButtonClicked()));
	QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(CancelButtonClicked()));
	QObject::connect(toolButton, SIGNAL(clicked()), this, SLOT(setExistingDirectory()));
}
RegistrationOpenDialog::~RegistrationOpenDialog()
{

}
void RegistrationOpenDialog::setExistingDirectory()
{
	QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
	QString directory = QFileDialog::getExistingDirectory(this,
		tr("Choose Project to Open"),
		lineEdit->text(),
		options);
	if (!directory.isEmpty())
		lineEdit->setText(directory);
}

void RegistrationOpenDialog::OKButtonClicked()
{
	while(lineEdit->text()== NULL)
	{
		QMessageBox::about(0,"ERROR!","Please choose a Registration project!");
		return;
	}

	
	QDir dir(lineEdit->text());
	if (!dir.exists()) 
	{
		QMessageBox::about(0,"ERROR!","The project path is not effective!");
		return;
	}	
	projectFullPath=dir.absolutePath();
/*
	projectName=projectFullPath.section('/',-1);
	if(!dir.exists(projectName+QString(".log")))
	{
		QMessageBox::about(0,"ERROR!","The project log file cannot be found!");
		return;
	}
	logFilePath=projectFullPath+QString("/")+projectName+QString(".log");*/

 	
 	dir.setFilter(QDir::Files);
 	dir.setSorting(QDir::Type);
 
 	QFileInfoList fileList = dir.entryInfoList();
 	if(fileList.size()<1)
 	{
		QMessageBox::about(0,"ERROR!","The project log file cannot be found!");
		return; 
 	}
 	bool log=false;


 	for (int i = 0; i < fileList.size(); ++i) 
	{
		QFileInfo fileInfo = fileList.at(i);
		if(fileInfo.isFile())
		{
			QString ext = fileInfo.suffix();
			if(ext==QString("xml"))
			{
				log=true;
				logFilePath=fileInfo.absoluteFilePath();
			}
		}		
 	}


	if(log==false)
	{
		QMessageBox::about(0,"ERROR!","The project log file cannot be found!");
		return;

	}
	isOKButtonClicked = true;
	accept();
}
void RegistrationOpenDialog::CancelButtonClicked()
{
	isOKButtonClicked = false;
	reject();
}
